package com.sk.imitationnetease.features.main

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.IBinder
import androidx.compose.runtime.Composable
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.ToastUtils
import com.sk.imitationnetease.IMediaClient
import com.sk.imitationnetease.IMediaService
import com.sk.imitationnetease.base.BaseActivity
import com.sk.imitationnetease.constants.Constant
import com.sk.imitationnetease.server.Api
import com.sk.imitationnetease.services.MediaPlayerService
import com.sk.skextension.getString
import com.sk.skextension.utils.net.retrofit.RetrofitClient
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

/**
 * 主界面activity
 */
class MainActivity : BaseActivity<MainView>() {
    var mediaService: IMediaService? = null

    override fun newInstanceView(): MainView = MainView()

    override fun initAndLoader() {
        checkStoragePermission()
        val mediaIntent = Intent(this, MediaPlayerService::class.java)
        bindService(mediaIntent, object : ServiceConnection {
            override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
                mediaService = IMediaService.Stub.asInterface(service)
                mediaService?.setMediaClient(object : IMediaClient.Stub() {
                    override fun onSuccess() {
                        log.info("绑定成功")
                    }
                })
            }

            override fun onServiceDisconnected(name: ComponentName?) {

            }

        }, Context.BIND_AUTO_CREATE)
        RetrofitClient.instance
            .createService(Api::class.java)
            .updateVer("海阔天空")
            .enqueue(object : Callback<String> {
                override fun onResponse(call: Call<String>, response: Response<String>) {
                    log.info(response.body().toString())
                }

                override fun onFailure(call: Call<String>, t: Throwable) {
                    log.info(t.toString())
                }

            })
    }

    /**
     * 获取存储读写权限
     */
    private fun checkStoragePermission() {
        if (!PermissionUtils.isGranted(*Constant.resquestRWStoragePermission)) {
            log.info("${Constant.resquestRWStoragePermission} 权限未授权开始请求")
            PermissionUtils.permission(*Constant.resquestRWStoragePermission)
                .callback(object : PermissionUtils.SimpleCallback {
                    override fun onGranted() {
                        log.info("${Constant.resquestRWStoragePermission.getString()} 权限已允许回调")
                    }

                    override fun onDenied() {
                        log.info("${Constant.resquestRWStoragePermission.getString()} 权限已绝回调")
                    }

                }).request()
        } else {
            log.info("${Constant.resquestRWStoragePermission.getString()} 权限已允许")
        }
    }

    fun showToast(msg: String) {
        ToastUtils.showShort(msg)
    }
}